package com.kuwou.workobject.Utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.kuwou.workobject.pojo.entity.Channel;
import com.kuwou.workobject.pojo.entity.Comment;
import com.kuwou.workobject.pojo.entity.Music;
import com.kuwou.workobject.pojo.entity.RankList;
import com.kuwou.workobject.service.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

public class CrawlRankListDataUtils {

    private static MusicService musicService;
    private static RankListService rankListService;
    private static ChannelService channelService;
    private static CommentService commentService;

    /**
     * 设置MusicService实现
     * @param musicService
     */
    public static void setMusicService(MusicService musicService) {
        CrawlRankListDataUtils.musicService = musicService;
    }

    /**
     * 设置RankListService实现
     * @param rankListService
     */
    public static void setRankListService(RankListService rankListService) {
        CrawlRankListDataUtils.rankListService = rankListService;
    }

    /**
     * 获取频道服务实现
     * @param channelService
     */
    public static void setChannelService(ChannelService channelService) {
        CrawlRankListDataUtils.channelService = channelService;
    }

    /**
     * 获取评论服务实现
     * @param commentService
     */
    public static void setCommentService(CommentService commentService) {
        CrawlRankListDataUtils.commentService = commentService;
    }

    public static void getData() throws IOException {
        //清空频道表
        channelService.truncateTable();
        // 保存频道
        channelService.saveBatch(UrlUtil.getChannelList());

        // 调用UrlUtil.getData方法获取音乐数据
        //Map<频道名称,Map<榜单名称,List<歌曲实体>>>
        Map<String, Map<String, List<Music>>> data = UrlUtil.getData("https://kuwo.cn/rankList");

        // 获取数据条目迭代器
        Iterator<Map.Entry<String, Map<String, List<Music>>>> iterator = data.entrySet().iterator();
        // 清空榜单表
        rankListService.truncateTable();

        //清空歌曲表
        musicService.truncateTable();

        //歌曲列表
        List<Music> musicList = new ArrayList<>();

        // 遍历数据条目
        while (iterator.hasNext()) {

            // 获取当前条目
            Map.Entry<String, Map<String, List<Music>>> entry = iterator.next();

            //频道名称
            String channelName = entry.getKey();

            //构建获取频道ID条件器
            LambdaQueryWrapper<Channel> queryWrapper =
                    new LambdaQueryWrapper<>(Channel.class)
                            .eq(Channel::getName, channelName);

            //获取频道实体
            Channel channel = Db.getOne(queryWrapper); // 正确：添加实体类参数

            // 频道ID
            int channelId = channel.getId();

            // 创建一个新的映射，只包含当前条目
            List<Music> musics = new ArrayList<>();


            // 获取当前频道的榜单
            Map<String, List<Music>> rankList = entry.getValue();

            // 创建一个新的map，只包含当前榜单，跟榜单下面的歌曲
            Map<RankList,List<Music>> rank = new HashMap<>();

            // 遍历当前频道的榜单
            Iterator<Map.Entry<String, List<Music>>> rankIterator = rankList.entrySet().iterator();
            System.out.println(channelName + "频道总共爬取了" + rankList.size() + "个榜单");
            while (rankIterator.hasNext()) {
                //获取榜单条目
                Map.Entry<String, List<Music>> ranks = rankIterator.next();

                String rankName = ranks.getKey();
                musicList = ranks.getValue();

                rank.put(new RankList(rankName, channelId, musicList.size(), LocalDateTime.now()),musicList);

            }

            List<RankList> rankLists = new ArrayList<>();


            rank.entrySet().iterator().forEachRemaining(ent -> {
                rankLists.add(ent.getKey());
            });

            // 保存榜单
            rankListService.saveBatch(rankLists);
            List<Music> musicLists = new ArrayList<>();
            Iterator<Map.Entry<RankList, List<Music>>> iteratored = rank.entrySet().iterator();
            while (iteratored.hasNext()) {
                Map.Entry<RankList, List<Music>> e = iteratored.next();
                RankList rankListTemp = e.getKey();
                int rankId = rankListTemp.getId();
                int rankChannelId = rankListTemp.getChannelId();
                List<Music> temp = e.getValue();

                temp.forEach(music -> {
                    music.setId(0);
                    music.setRankListId(rankId);
                    music.setChannelId(rankChannelId);
                });
                musicLists.addAll(temp);
            }
            musicService.saveBatch(musicLists,musicLists.size());
        }
    }

    public static void getComment(){
        List<RankList> rankLists = rankListService.list();
        List<Comment> comment = UrlUtil.getComment(rankLists);
        commentService.truncateTable();
        commentService.saveBatch(comment);
    }

}
